package com.example.todo;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    EditText ed_name;
    EditText ed_password;
    Button btn_login;
    Button btn_reg;
    String userName;
    String IdInput;
    String PhoneInput;
    String Password;
    RadioGroup radioGroup;
    RadioButton rb_phone;
    RadioButton rb_id;
    SQLiteDatabase db;
    DBOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        helper = new DBOpenHelper(this);
        //控件获取
        ed_name = findViewById(R.id.ed_Name);
        ed_password = findViewById(R.id.ed_Password);
        btn_login = findViewById(R.id.btn_login);
        btn_reg = findViewById(R.id.btn_reg);
        radioGroup = findViewById(R.id.radioGroup);
        rb_phone = findViewById(R.id.rb_phone);
        rb_id = findViewById(R.id.rb_id);
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
    }

    //记住登录信息
    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        ed_name.setText(sp.getString("account", ""));
        ed_password.setText(sp.getString("password", ""));
    }

    @SuppressLint("Range")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String name = ed_name.getText().toString();
                String password = ed_password.getText().toString();
                if (name.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                //从数据库中获取信息
                db = helper.getReadableDatabase();
                //通过判断是学号登陆还是手机号登录来获取不同的信息比对
                if (rb_phone.isChecked()) {
                    Cursor cursor = db.query("user", null, "phone=?", new String[]{name}, null, null, null);
                    if (cursor.getCount() > 0) {
                        cursor.moveToNext();//结果集中的指针指向下一个位置
                        PhoneInput = cursor.getString(cursor.getColumnIndex("phone"));
                        Password = cursor.getString(cursor.getColumnIndex("password"));
                    }
                    userName = PhoneInput;
                }
                if (rb_id.isChecked()) {
                    Cursor cursor = db.query("user", null, "id=?", new String[]{name}, null, null, null);
                    if (cursor.getCount() > 0) {
                        cursor.moveToNext();//结果集中的指针指向下一个位置
                        IdInput = cursor.getString(cursor.getColumnIndex("id"));
                        Password = cursor.getString(cursor.getColumnIndex("password"));
                    }
                    userName = IdInput;
                }
                db.close();
                //如果密码和账号都正确的话可以登录，并且记住密码
                if (name.equals(userName) && password.equals(Password)) {
                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("account", name);
                    editor.putString("password", password);
                    editor.commit();
                    finish();

                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
                break;
            //跳转到注册界面
            case R.id.btn_reg:
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

}